<?php
class HopeModule extends dpModule
{
	//用户
	protected $user;

	//站点ID
	protected $site_id;

	//令牌验证
	protected $encrypt;

	//session
	protected $session;

	//布局
	public $layout = 'main';

	//通用提示信息
	protected $msg = array(
		'no_1'  => '参数验证失败，非法操作',
		'no_2'  => '缺少参数，操作失败',
		'no_3'  => '保存失败，请重试',
		'no_4'  => '删除失败',
		'no_5'  => '添加失败，请重试',

		'yes_1' => '保存成功',
		'yes_2' => '删除成功',
		'yes_3' => '添加成功',
		'yes_4' => '修改成功',
	);

	/**
	 * 公共action
	 */
	public function actions()
	{
		return array(
			//图片上传
			'uploadimage' => 'admin:action.UpLoadImageAction',
			//图片管理
			'imagemanage' => 'admin:action.ImageManageAction',
		);
	}

	//后台初始化
	public function init()
	{
		//用户初始化
		$this->user = D::app()->user();
		$this->user->loginUrl = D::createUrl('admin/index/login');
		$this->user->setKeyPrefix(D::$config['cookie']);

		//session初始化
		$this->session = new dpSession();

		if (!$this->user->checkAccess($this->getAction(), 'user', true) && !$this->user->isGuest()) {
			$this->error('没有权限操作！');
		}

		//获取站点ID
		$this->site_id = $this->session->get('admin_site_id') ? $this->session->get('admin_site_id') : 1;

		//延迟加载后台model目录下所有模型
		D::import('admin:model.*');

		//FORM身份令牌
		$this->encrypt = new dpEncrypt();

		//模板推送全局变量
		$this->assign('page_title', 'HopeCMS内容管理系统 - ' . POWERED);
		$this->assign('root', $this->user->getName());
		$this->assign('site_name', siteModel::model()->getCurrentSiteName($this->site_id));
		$this->assign('sites', siteModel::model()->getSite());
		$this->assign('top_menu', menuModel::model()->getTopMenu());
	}

	//获取左边子菜单
	public function actionGetChildMenu($menuId = 0)
	{
		$this->ajaxReturn(menuModel::model()->getChildMenu($menuId));
	}

	//错误处理
	public function error($msg)
	{
		D::error($msg);
	}

	//重写__action方法
	public function __action()
	{
		if (strpos('menu', $this->getAction()) !== false) {
			parent::__action();
		} else {
			exit('');
		}
	}

	//获取保存按钮
	protected function getSaveButton($url, $formName, $button='保存', $true = 1)
	{
		return '<a href="javascript:;" onclick="save(\''.$url.
			'\', \''.$formName.'\', '.$true.');" class="link top_button label-hover label-activ">'.$button.'</a>';
	}

	//获取新增按钮
	protected function getCreateButton($url, $button='新增', $url2='')
	{
		if (!empty($url2)) {
			$url2 = '&'.preg_replace('/'.str_replace('/', '\/', D::homeUrl()).'index\.php\?m=(\w+)&c=(\w+)&a=(\w+)&/i', '', $url2);
		}
		return '<a href="javascript:;" onclick="create(\''.$url.'Add'.$url2.'\');" class="link top_button label-hover label-activ">'.$button.'</a>';
	}

	//返回按钮
	protected function getBackButton($url, $url2)
	{
		return '<a href="javascript:;" onclick="goback(\''.$url.'\', \''.$url2.'\');" class="link top_button label-hover label-activ">返回</a>';
	}

	//获取$_GET的值
	protected function action()
	{
		return isset($_GET['act']) ? $_GET['act'] : '';
	}

	protected function ajaxInfo($code, $msg, $url = '')
	{
		return array('code'=>$code, 'msg'=>$msg, 'url'=>$url);
	}

	protected function ajaxGoto($msg, $links)
	{
		return array(
			'code'	 => 2,
			'msg'	 => $msg,
			'link_1' => $links[0],
			'text_1' => $links[1],
			'link_2' => $links[2],
			'text_2' => $links[3],
		);
	}


	//后台身份统一验证规则
	public function rules()
	{
		return array(
			
			array(
				'access'  => 'allow',
				'control' => array('index'),
				'actions' => array('login','logout'),
				'user'    => '?',
			),

			array(
				'access'  => 'allow',
				'control' => array('*'),
				'actions' => array('*'),
				'user'    => '@',
			),
			
		);
	}
	
}
?>